int TreeSize(struct TreeNode* root)
{
    if (root == NULL)
        return 0;
    return TreeSize(root->left) + TreeSize(root->right) + 1;
}

void prevorder(struct TreeNode* root, int* a, int* i)
{
    if (root == NULL)
        return;
    a[*i] = root->val;
    *i = (*i) + 1;
    prevorder(root->left, a, i);
    prevorder(root->right, a, i);
}


int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int n = TreeSize(root);
    int* a = (int*)malloc(sizeof(int) * n);
    *returnSize = n;
    int q = 0;
    prevorder(root, a, &q);
    return a;
}
